<template>
  <view class="wrapper">
    <!-- 商品图片 -->
    <swiper
      class="pics"
      indicator-dots
      indicator-color="rgba(255, 255, 255, 0.6)"
      indicator-active-color="#fff"
    >
      <swiper-item  :key="sw.pics_id" v-for="sw in detail.pics">
        <image :src="sw.pics_mid_url" />
      </swiper-item>
    </swiper>
    <!-- 基本信息 -->
    <view class="meta">
      <view class="price">￥{{detail.goods_price || '--'}}</view>
      <view class="name">{{detail.goods_name || '--'}}</view>
      <view class="shipment">快递: 免运费</view>
      <text class="collect icon-star">收藏</text>
    </view>
    <!-- 商品详情 -->
    <view class="detail">
       <!--  专门处理富文本节点字符串 -->
      <!-- <rich-text :nodes="detail.goods_introduce"></rich-text> -->
      <!-- <view v-html="detail.goods_introduce"></view> -->
      <jyf-parser :html="detail.goods_introduce"></jyf-parser>
    </view>
    <!-- 操作 -->
    <view class="action">
      <button open-type="contact" class="icon-handset">联系客服</button>
      <text class="cart icon-cart" @click="goCart">购物车</text>
      <!-- 显示购物车商品数量 -->
      <text class="cart-count" v-if="carts.length">{{carts.length}}</text>
      
      <text @click="addCart" class="add">加入购物车</text>
      <text class="buy" @click="createOrder">立即购买</text>
    </view>
  </view>
</template>

<script>
import jyfParser from "@/components/jyf-parser/jyf-parser"
export default {
  data() {
    // 商品详情数据
    return {
      detail: null,

      // 购物车数据  优先读取本地数据
      carts:uni.getStorageSync('carts') || []
    }
  },

  components: {
    // 注册富文本组件
    jyfParser
  },

  onLoad(qu){
    this.getDetail(qu.id)
  },
  methods: {
    // 加入购物车
    addCart() {
      // 不需要登录就可以使用购物车
      // 1.加入购物车当前商品可以多次加入 如果重复数量加一
      // 2.添加完 存储到本地

      let isAdd = this.carts.some((item)=>{
        if (item.goods_id === this.detail.goods_id) {
          // 当前商品已经在购物车存在
          item.goods_count +=1
          uni.showToast({
            title:`${this.detail.goods_name}数量+1`,
            icon:'none',
            duration:2000
          })
          return true
        }
      })
      // 不存在  购物车要新增商品
      if (!isAdd) {
        // 获取当前商品必要数据
        const{goods_id,goods_name,goods_price,goods_small_logo} = this.detail

        // 页面存储
        this.carts.push({
          goods_id,goods_name,goods_price,goods_small_logo,
          // 商品数量
          goods_count:1,
          // 购物车选中状态
          goods_checked:true
        })
      }

      // 本地存储
      uni.setStorageSync('carts',this.carts)

    },
    // 获取商品详情数据
    async getDetail(goods_id) {
      const {msg,data} = await this.request({url:'/goods/detail',data:{goods_id}})
      console.log(msg,data);

      if (msg.status === 200) {
        this.detail = data
      }
    },
    goCart() {
      uni.switchTab({
        url: "/pages/cart/index"
      });
    },
    createOrder() {
      uni.navigateTo({
        url: "/pages/order/index"
      });
    }
  }
};
</script>

<style scoped lang="scss">
.wrapper {
  margin-bottom: 100rpx;
  background-color: #f4f4f4;
}

.pics {
  height: 640rpx;
}

.meta {
  height: 250rpx;
  line-height: 1;
  padding: 30rpx 180rpx 30rpx 20rpx;
  box-sizing: border-box;
  background-color: #fff;
  position: relative;

  .price {
    font-size: 36rpx;
    color: #ea4451;
    margin-bottom: 20rpx;
  }

  .name {
    color: #333;
    line-height: 1.4;
    font-size: 33rpx;

    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }

  .shipment {
    font-size: 27rpx;
    color: #999;
    position: absolute;
    bottom: 30rpx;
  }

  .collect {
    width: 140rpx;
    height: 88rpx;
    text-align: center;
    box-sizing: border-box;
    border-left: 1rpx solid #ddd;
    font-size: 24rpx;
    color: #999;

    position: absolute;
    right: 10rpx;
    top: 91rpx;
  }

  [class*="icon-"]::before {
    display: block;
    font-size: 45rpx;
    margin-bottom: 10rpx;
  }
}

.detail image {
  width: 100%;
  height: 480rpx;
  margin-top: 20rpx;
}

.action {
  width: 100%;
  height: 98rpx;
  background-color: #fff;

  position: fixed;
  left: 0;
  bottom: 0;

  display: flex;
  align-items: center;

  // 购物车数量
  .cart-count {
    position: absolute;
    top: -10rpx;
    left: 260rpx;
    width: 50rpx;
    height: 50rpx;
    font-size: 24rpx;
    border-radius: 50rpx;
    line-height: 50rpx;
    text-align: center;
    background-color: red;
    color: #fff;
  }

  text {
    display: block;
  }

  .add,
  .buy {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 210rpx;
    text-align: center;
    font-size: 27rpx;
    color: #fff;
  }

  .add {
    background-color: #f4b73f;
  }

  .buy {
    background-color: #ea4451;
  }

  button {
    padding: 0;
    border-radius: 0;
    background-color: #fff;

    &::after {
      border: none;
    }
  }

  button,
  .cart {
    flex: 1;
    text-align: center;
    color: #989898;
    font-size: 24rpx;
    box-sizing: border-box;
  }

  [class*="icon"]::before {
    display: block;
    font-size: 45rpx;
    margin-bottom: 2rpx;
  }
}
</style>
